package com.chinaisafe.demo.service.impl;

import com.chinaisafe.demo.entity.DemoTable;
import com.chinaisafe.demo.mapper.DemoTableMapper;
import com.chinaisafe.demo.service.DemoTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.demo.vo.request.DemoTableReqVO;
import com.chinaisafe.demo.vo.response.DemoTableRespVO;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * DemoTableServiceImpl
 * Description：测试类服务实现类
 *
 * @author JiangZhaoYong
 * @date 2024-07-15 13:00:14
 */
@Service
public class DemoTableServiceImpl extends ServiceImpl<DemoTableMapper, DemoTable> implements DemoTableService {

    @Override
    public IPage<DemoTableRespVO> getPageList(DemoTableReqVO demoTableReqVO) {
        // 分页设置
        Page<DemoTable> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), demoTableReqVO);
        // 查询条件构造
        QueryWrapper<DemoTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DemoTable::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DemoTableRespVO add(DemoTableReqVO demoTableReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(demoTableReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DemoTableRespVO update(DemoTableReqVO demoTableReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(demoTableReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DemoTableRespVO addOrUpdate(DemoTableReqVO demoTableReqVO) {
        Long demoTableId = demoTableReqVO.getId();
        if (demoTableId != null){
            // 编辑
            DemoTable demoTable = baseMapper.selectById(demoTableId);
            Optional.ofNullable(demoTable).orElseThrow(() -> BizException.build("未找到要修改的测试类信息"));
        }
        DemoTable demoTable2Save = DemoTable.convertFromReqVo(demoTableReqVO);
        boolean success = this.saveOrUpdate(demoTable2Save);
        if (success) {
            return demoTable2Save.convertToRespVO();
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public DemoTableRespVO getDetailById(Long demoTableId) {
        DemoTable demoTable = baseMapper.selectById(demoTableId);
        Optional.ofNullable(demoTable).orElseThrow(() -> BizException.build("未找到对应测试类信息"));
        return demoTable.convertToRespVO();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] demoTableIds) {
        if (demoTableIds.length > 0) {
            this.removeByIds(Arrays.asList(demoTableIds));
        }
    }
}
